package so.wy.servicecenter.businessbed.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.check.CommonWebCheck;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.exception.WebException;
import so.wy.servicecenter.base.frame.baseopt.BaseOptService;
import so.wy.servicecenter.base.frame.baseopt.BaseOptWeb;
import so.wy.servicecenter.base.frame.web.webhand.WebExceptionHandler;
import so.wy.servicecenter.businessbed.service.device.DeviceDTO;
import so.wy.servicecenter.businessbed.service.device.DeviceDetailsDTO;
import so.wy.servicecenter.businessbed.service.device.DeviceService;


@Service
@Scope("prototype")
public class DeviceWeb extends BaseOptWeb<DeviceDTO> {
    @Autowired
    private DeviceService deviceService;

    @Override
    protected BaseOptService getOptService() {
        return deviceService;
    }

    @WebExceptionHandler
    public void updateDeviceRunInfo(String deviceSerial, Double batteryBoard, Double batteryLock, String hardVer, String softVer) {
        deviceService.updateDeviceRunInfo(deviceSerial, batteryBoard, batteryLock, hardVer, softVer);
    }

    @WebExceptionHandler
    public DeviceDetailsDTO getDeviceInfoBySerial(String deviceSerial) throws WebException {
        return deviceService.getDeviceInfoBySerial(deviceSerial);
    }

    @WebExceptionHandler
    public void publishDevice(Long deviceId, Long buildingId, Long departId) throws WebException {
        CommonWebCheck.validateNotEmpty(departId, "设备信息不能为空");
        CommonWebCheck.validateNotEmpty(buildingId, "位置信息不能为空");
        CommonWebCheck.validateNotEmpty(departId, "科室信息不能为空");

        try {
            deviceService.publishDevice(deviceId, buildingId, departId);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }

    @WebExceptionHandler
    public void publishDownDevice(Long deviceId) throws WebException {
        CommonWebCheck.validateNotEmpty(deviceId, "设备信息不能为空");

        try {
            deviceService.publishDownDevice(deviceId);
        } catch (ServiceException e) {
            throw exception(e);
        }
    }
}
